package com.projectplace.GUI;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.GUI.adapter.Contacts_Adapter;
import com.projectplace.api.UserApi;
import com.projectplace.api.objectResponses.ppApiCoWorkers;
import com.projectplace.api.objectResponses.ppApiUser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class Contacts extends Activity {

	private static Activity activity;
	private ppApiCoWorkers coWorkers;
	private static Contacts_Adapter contactAdapater;
	private final String LOG_TAG = "Contacts";
	private GridView grid;
	
	
	private Handler mHandler = new Handler(){
		
		
		/*
		 * (non-Javadoc) This could be a more beautiful solution instead of catching ClassCastException in CallbackWrapper
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
	    @Override
	    public void handleMessage(Message msg) {
	    	Log.d(LOG_TAG, LOG_TAG);
	        Log.d(LOG_TAG, Integer.toString(msg.what));
	        Log.d(LOG_TAG, Integer.toString(msg.arg1));
	        Log.d(LOG_TAG, Integer.toString(msg.arg2));
	        Log.d(LOG_TAG +" Dataobtained", msg.toString());
	        super.handleMessage(msg);
	    }
	    
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = this;
		setContentView(R.layout.grid);
		
		grid =(GridView) findViewById(R.id.grid);
		grid.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ppApiUser u = coWorkers.getCoWokerByIndex(arg2);
				Log.d(LOG_TAG, u.getID());
				Intent intent = new Intent().setClass(activity, ContactsProfile.class);
				intent.putExtra("first", u.getFirst_Name());
				intent.putExtra("last", u.getLast_Name());
				intent.putExtra("phone", u.getMobile_Phone());
				intent.putExtra("email", u.getEmail());
				intent.putExtra("description", u.getDescription());
				intent.putExtra("userID", u.getID());
				startActivity(intent);
			}
		});
		
		registerForContextMenu(grid);

	} 
	
	/*@Override
	public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuinfo){
		super.onCreateContextMenu(menu, v, menuinfo);
		  MenuInflater inflater = getMenuInflater();
		  inflater.inflate(R.menu.pp_menu, menu);
	}*/
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	  Log.d(LOG_TAG,Integer.toString(info.position));
	  Log.d(LOG_TAG, coWorkers.getCoWokerByIndex(info.position).getFirst_Name() +" " +coWorkers.getCoWokerByIndex(info.position).getLast_Name());
	  switch (item.getItemId()) {
	  case R.id.editItem:
	    Log.d(LOG_TAG, "Edit item");
	    return true;
	  case R.id.deleteItem:
	    Log.d(LOG_TAG, "Delete Item");
	    return true;
	  case R.id.favoriteItem:
		  Log.d(LOG_TAG, "Favotie");
		  return true;
	  case R.id.shareItem:
		  Log.d(LOG_TAG, "Share item");
		  return true;
	  case R.id.viewItem:
		  Log.d(LOG_TAG, "View item");
	  default:
	    return super.onContextItemSelected(item);
	  }
	}
	
	public void onResume() {
		super.onResume(); 
		UserApi.getCoWorkers(
				SingeltonPPClient.getInstance(),
				mHandler,
				new com.projectplace.api.Callback.Callback.ResponseListenerGetCoWorkers() {
					public void SERVER_ERROR(Error err) {
						Log.d(LOG_TAG, "Server error");
					}

					public void BAD_TOKEN_ERROR(Error err) {
						Log.d(LOG_TAG, err.getMessage());
					}

					public void onReceive(ppApiCoWorkers user) {
						if(coWorkers == null){
							coWorkers = user;
							contactAdapater = new Contacts_Adapter(activity,coWorkers);
							grid.setAdapter(contactAdapater);
						}
					}
				});
	}
	@Override
	public void onPause(){
		super.onPause();
	
	}
	
	@Override
	public void onRestart(){
		super.onRestart();
		Log.d(LOG_TAG, "RESTARTING ACTIVITY");
	}
	
}
